const fs = require('fs');

class Logger {

    constructor() {
        this.logPath = '/log/websocket-iot-server/logs';
    }

    log(tag, msg, print = false){
        const date = new Date().toLocaleDateString('zh-cn').replaceAll("/", "-")

        const log = `${this.logPath}/event-${date}.log`
        const time = new Date().toLocaleString('zh-cn');

        const logLine = `${time}-[${tag}]: ${msg}\n`
        
        if (print || process.env.NODE_ENV === 'development') {
            console.debug(logLine)
        }
        
        fs.appendFile(log, logLine, (err) => {
            if (err) {
                console.error(err)
            }
        });
    }
}

const logger = new Logger();

module.exports = logger